package com.zyn.rsgl.controller;


import com.zyn.rsgl.mapper.ScheduleDetailMapper;
import com.zyn.rsgl.mapper.ScheduleMapper;
import com.zyn.rsgl.pojo.Approval;
import com.zyn.rsgl.pojo.Overtime;
import com.zyn.rsgl.pojo.ScheduleDetail;
import com.zyn.rsgl.service.OvertimeService;
import com.zyn.rsgl.utils.commonResult.R;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

/**
 * <p>
 * 请假表 前端控制器
 * </p>
 *
 * @author zyn
 * @since 2024-04-10
 */
@RestController
@RequestMapping("/rsgl/overtime")
public class OvertimeController {
    @Resource
    private OvertimeService overtimeService;

    @Resource
    private ApprovalController approvalController;

    @Resource
    private ScheduleDetailMapper scheduleDetailMapper;

    // 分页查询
    @GetMapping("/list")
    public R list(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) String userId
    ) {
        return overtimeService.listPage(pageNum, pageSize, startTime, status, userId);
    }

    // 获取单个请假信息
    @GetMapping("/get/{overtimeId}")
    public R get(@PathVariable("overtimeId") String overtimeId) {
        Overtime byId = overtimeService.getById(overtimeId);
        if (byId != null)
            return R.ok(byId);
        return R.fail("请假信息不存在");
    }

    // 添加请假信息
    @PostMapping("/add")
    public R add(@RequestBody Overtime overtime) {
        // 判断是否存在
        if (overtimeService.getById(overtime.getId()) != null)
            return R.fail("请假信息已存在");

        // 获取申请日期
        LocalDateTime startTime = overtime.getStartTime();
        LocalDateTime endTime = overtime.getEndTime();

        DayOfWeek dayOfWeek = startTime.getDayOfWeek();

        // 查询当天的排班详细信息
        ScheduleDetail scheduleDetail = scheduleDetailMapper.getScheduleDetailByDayOfWeek(overtime.getUserId(), getChineseDayOfWeek(dayOfWeek));

        // 校验加班时段是否在上班时段内
        LocalTime workStartTime = scheduleDetail.getStartTime();
        LocalTime workEndTime = scheduleDetail.getEndTime();

        if (startTime.toLocalTime().isBefore(workEndTime) && endTime.toLocalTime().isAfter(workStartTime)) {
            return R.fail("申请的加班时段与上班时段冲突");
        }

        overtime.setStatus("待审批");
        boolean isSuccess = overtimeService.save(overtime);
        if(isSuccess){
            Approval approval = new Approval();
            approval.setOriginalId(overtime.getId());
            approval.setUserId(overtime.getUserId());
            approval.setType("加班");
            approval.setReason(overtime.getReason());
            approval.setStartTime(overtime.getStartTime());
            approval.setEndTime(overtime.getEndTime());
            approval.setDuration(overtime.getDuration());
            approval.setStatus("待审批");
            approvalController.add(approval);
            return R.ok();
        }
        return R.fail("添加失败");
    }

    // 修改请假信息
    @PutMapping("/update")
    public R update(@RequestBody Overtime overtime) {
        //校验请假天数
        if (overtime.getDuration()<1){
            return R.fail("请假天数错误！");
        }
        return overtimeService.updateById(overtime) ? R.ok() : R.fail("修改失败");
    }

    // 删除请假信息
    @DeleteMapping("/delete/{overtimeId}")
    public R delete(@PathVariable("overtimeId") String overtimeId) {
        return overtimeService.removeById(overtimeId) ? R.ok() : R.fail("删除失败");
    }

    public static String getChineseDayOfWeek(DayOfWeek dayOfWeek) {
        switch (dayOfWeek) {
            case MONDAY:
                return "星期一";
            case TUESDAY:
                return "星期二";
            case WEDNESDAY:
                return "星期三";
            case THURSDAY:
                return "星期四";
            case FRIDAY:
                return "星期五";
            case SATURDAY:
                return "星期六";
            case SUNDAY:
                return "星期日";
            default:
                return "未知";
        }
    }
}
